package com.stlm2.dems.service;

import com.stlm2.dems.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.dao.CardsDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("cardsService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class CardsService extends BaseServiceImpl<Cards> {

	@Resource
	private CardsDao cardsDao;
	@Resource
	private DdmGoodsService ddmGoodsService;
	@Resource
	private GoodsCategoryService goodsCategoryService;
	@Resource
	private UserCardsService userCardsService;

	@Override
	protected BaseDao <Cards> getDao() {
		return cardsDao;
	}

	public List<ModelMap> convert(List<Cards> cards) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (cards != null && cards.size() > 0) {
			for (Cards cars : cards) {
				ModelMap map = buildModel(cars);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(Cards cards) {
		ModelMap model = new ModelMap();
		model.put("id",cards.getId());
		model.put("name",cards.getName());
		model.put("cardType", cards.getCardsType().toString());
		model.put("money",cards.getMoney());
		model.put("validDay",cards.getValidDay());
		model.put("img","../phoneImg/index_coupon_icon.png");

		if(cards.getGoodsId()!=null){
			DdmGoods ddmGoods=ddmGoodsService.findEntityById(cards.getGoodsId());
			model.put("name",ddmGoods.getName()+"类");
			model.put("img",ddmGoods.getMainImg().split(",")[0]);
		}

		if(cards.getGoodsCategoryId()!=null){
			GoodsCategory goodsCategory=goodsCategoryService.findEntityById(cards.getGoodsCategoryId());
			model.put("name",goodsCategory.getName()+"类");
			model.put("img",goodsCategory.getImgUrl());
		}
		return model;
	}

	public List<ModelMap> convert(List<Cards> cards, User user) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (cards != null && cards.size() > 0) {
			for (Cards cars : cards) {
				ModelMap map = buildModel(cars,user);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(Cards cards,User user) {
		ModelMap model = new ModelMap();
		model.put("id",cards.getId());
		model.put("name",cards.getName());
		model.put("cardType", cards.getCardsType().toString());
		model.put("money",cards.getMoney());
		model.put("validDay",cards.getValidDay());
		model.put("img","../phoneImg/index_coupon_icon.png");

		if(cards.getGoodsId()!=null){
			DdmGoods ddmGoods=ddmGoodsService.findEntityById(cards.getGoodsId());
			model.put("name",ddmGoods.getName());
			model.put("img",ddmGoods.getMainImg().split(",")[0]);
		}

		if(cards.getGoodsCategoryId()!=null){
			GoodsCategory goodsCategory=goodsCategoryService.findEntityById(cards.getGoodsCategoryId());
			model.put("name",goodsCategory.getName()+"类");
			model.put("img",goodsCategory.getImgUrl());
		}
		model.put("isGet",0);
		if(user!=null){
			UserCards userCards=new UserCards();
			userCards.setCardsId(cards.getId());
			userCards.setUserId(user.getId());
			UserCards userCard=userCardsService.findEntityByUserCards(userCards);
			if(userCard!=null)
				model.put("isGet",1);
		}
		return model;
	}

}